package com.makeblock.demo.testdatabinding.viewmodel;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.makeblock.demo.testdatabinding.BR;
import com.makeblock.demo.testdatabinding.model.DataModel;

/**
 * Created by xuexin on 2016/10/27.
 */

public class ItemViewModel extends BaseObservable {

    private DataModel model;

    public ItemViewModel(DataModel model) {
        this.model = model;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof DataModel) {
            return obj.equals(model);
        }
        return super.equals(obj);
    }

    @Bindable
    public String getText() {
        return model.getText();
    }

    @Bindable
    public String getHideString() {
        return model.getHideString();
    }

    @Bindable
    public boolean getShow() {
        return model.getShow();
    }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        model.setShow(isChecked);
        notifyPropertyChanged(BR.show);
        notifyPropertyChanged(BR.text);
    }

    public void onClickItem(View view) {
        Toast.makeText(view.getContext(), model.getText().toString() + "被点击了", Toast.LENGTH_SHORT).show();
    }

    public void setModel(DataModel model) {
        this.model = model;
        notifyPropertyChanged(BR.show);
        notifyPropertyChanged(BR.text);
        notifyPropertyChanged(BR.hideString);
    }
}
